{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "436fbff0-9783-4e4f-a480-d7d8e9251f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac5ba365-8e45-413f-b081-b99f889294a2",
   "metadata": {},
   "source": [
    "## 进行数据分箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f141051e-52f0-44a0-834c-f6aac9f10523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>居住地</th>\n",
       "      <th>年龄</th>\n",
       "      <th>工资</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>男</td>\n",
       "      <td>北京</td>\n",
       "      <td>38</td>\n",
       "      <td>18053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>上海</td>\n",
       "      <td>42</td>\n",
       "      <td>9382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>男</td>\n",
       "      <td>广州</td>\n",
       "      <td>23</td>\n",
       "      <td>6376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>女</td>\n",
       "      <td>深圳</td>\n",
       "      <td>36</td>\n",
       "      <td>10746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>杭州</td>\n",
       "      <td>20</td>\n",
       "      <td>5284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>女</td>\n",
       "      <td>南京</td>\n",
       "      <td>34</td>\n",
       "      <td>9828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>成都</td>\n",
       "      <td>33</td>\n",
       "      <td>9366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>男</td>\n",
       "      <td>重庆</td>\n",
       "      <td>47</td>\n",
       "      <td>22820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>男</td>\n",
       "      <td>武汉</td>\n",
       "      <td>36</td>\n",
       "      <td>16927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>女</td>\n",
       "      <td>西安</td>\n",
       "      <td>42</td>\n",
       "      <td>11591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>男</td>\n",
       "      <td>南昌</td>\n",
       "      <td>42</td>\n",
       "      <td>11316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>男</td>\n",
       "      <td>合肥</td>\n",
       "      <td>27</td>\n",
       "      <td>7426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>男</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23</td>\n",
       "      <td>4705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>男</td>\n",
       "      <td>福州</td>\n",
       "      <td>41</td>\n",
       "      <td>24117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>女</td>\n",
       "      <td>厦门</td>\n",
       "      <td>50</td>\n",
       "      <td>69153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>女</td>\n",
       "      <td>济南</td>\n",
       "      <td>32</td>\n",
       "      <td>5559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>男</td>\n",
       "      <td>郑州</td>\n",
       "      <td>56</td>\n",
       "      <td>6391</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>男</td>\n",
       "      <td>长春</td>\n",
       "      <td>60</td>\n",
       "      <td>11020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>男</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>59</td>\n",
       "      <td>68189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>女</td>\n",
       "      <td>南宁</td>\n",
       "      <td>32</td>\n",
       "      <td>15661</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  居住地  年龄     工资\n",
       "0   男   北京  38  18053\n",
       "1   女   上海  42   9382\n",
       "2   男   广州  23   6376\n",
       "3   女   深圳  36  10746\n",
       "4   男   杭州  20   5284\n",
       "5   女   南京  34   9828\n",
       "6   男   成都  33   9366\n",
       "7   男   重庆  47  22820\n",
       "8   男   武汉  36  16927\n",
       "9   女   西安  42  11591\n",
       "10  男   南昌  42  11316\n",
       "11  男   合肥  27   7426\n",
       "12  男   长沙  23   4705\n",
       "13  男   福州  41  24117\n",
       "14  女   厦门  50  69153\n",
       "15  女   济南  32   5559\n",
       "16  男   郑州  56   6391\n",
       "17  男   长春  60  11020\n",
       "18  男  哈尔滨  59  68189\n",
       "19  女   南宁  32  15661"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.read_csv(\"residents_data.csv\")\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c093825f-8571-4e44-92b2-8988053c6724",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "年龄\n",
       "20     5284.0\n",
       "23     5540.5\n",
       "27     7426.0\n",
       "32    10610.0\n",
       "33     9366.0\n",
       "34     9828.0\n",
       "36    13836.5\n",
       "38    18053.0\n",
       "41    24117.0\n",
       "42    10763.0\n",
       "47    22820.0\n",
       "50    69153.0\n",
       "56     6391.0\n",
       "59    68189.0\n",
       "60    11020.0\n",
       "Name: 工资, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.groupby(\"年龄\")[\"工资\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7ee290b-daa3-4344-aaed-d25b056eab96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     (30, 40]\n",
       "1     (40, 50]\n",
       "2     (20, 30]\n",
       "3     (30, 40]\n",
       "4     (10, 20]\n",
       "5     (30, 40]\n",
       "6     (30, 40]\n",
       "7     (40, 50]\n",
       "8     (30, 40]\n",
       "9     (40, 50]\n",
       "10    (40, 50]\n",
       "11    (20, 30]\n",
       "12    (20, 30]\n",
       "13    (40, 50]\n",
       "14    (40, 50]\n",
       "15    (30, 40]\n",
       "16    (50, 60]\n",
       "17    (50, 60]\n",
       "18    (50, 60]\n",
       "19    (30, 40]\n",
       "Name: 年龄, dtype: category\n",
       "Categories (7, interval[int64, right]): [(0, 10] < (10, 20] < (20, 30] < (30, 40] < (40, 50] < (50, 60] < (60, 120]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 定义年龄分组列表：[0, 10, 20, 30, 40, 50, 60, 120]\n",
    "# 2. 并根据以上分组对df1的年龄列进行分箱\n",
    "age_bins = [0, 10, 20, 30, 40, 50, 60, 120]\n",
    "pd.cut(df1[\"年龄\"], age_bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c14ffad3-7dda-4a58-9dfe-67b5969661da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>居住地</th>\n",
       "      <th>年龄</th>\n",
       "      <th>工资</th>\n",
       "      <th>年龄组</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>男</td>\n",
       "      <td>北京</td>\n",
       "      <td>38</td>\n",
       "      <td>18053</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>上海</td>\n",
       "      <td>42</td>\n",
       "      <td>9382</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>男</td>\n",
       "      <td>广州</td>\n",
       "      <td>23</td>\n",
       "      <td>6376</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>女</td>\n",
       "      <td>深圳</td>\n",
       "      <td>36</td>\n",
       "      <td>10746</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>杭州</td>\n",
       "      <td>20</td>\n",
       "      <td>5284</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>女</td>\n",
       "      <td>南京</td>\n",
       "      <td>34</td>\n",
       "      <td>9828</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>成都</td>\n",
       "      <td>33</td>\n",
       "      <td>9366</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>男</td>\n",
       "      <td>重庆</td>\n",
       "      <td>47</td>\n",
       "      <td>22820</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>男</td>\n",
       "      <td>武汉</td>\n",
       "      <td>36</td>\n",
       "      <td>16927</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>女</td>\n",
       "      <td>西安</td>\n",
       "      <td>42</td>\n",
       "      <td>11591</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>男</td>\n",
       "      <td>南昌</td>\n",
       "      <td>42</td>\n",
       "      <td>11316</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>男</td>\n",
       "      <td>合肥</td>\n",
       "      <td>27</td>\n",
       "      <td>7426</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>男</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23</td>\n",
       "      <td>4705</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>男</td>\n",
       "      <td>福州</td>\n",
       "      <td>41</td>\n",
       "      <td>24117</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>女</td>\n",
       "      <td>厦门</td>\n",
       "      <td>50</td>\n",
       "      <td>69153</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>女</td>\n",
       "      <td>济南</td>\n",
       "      <td>32</td>\n",
       "      <td>5559</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>男</td>\n",
       "      <td>郑州</td>\n",
       "      <td>56</td>\n",
       "      <td>6391</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>男</td>\n",
       "      <td>长春</td>\n",
       "      <td>60</td>\n",
       "      <td>11020</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>男</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>59</td>\n",
       "      <td>68189</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>女</td>\n",
       "      <td>南宁</td>\n",
       "      <td>32</td>\n",
       "      <td>15661</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  居住地  年龄     工资  年龄组\n",
       "0   男   北京  38  18053   壮年\n",
       "1   女   上海  42   9382   中年\n",
       "2   男   广州  23   6376   青年\n",
       "3   女   深圳  36  10746   壮年\n",
       "4   男   杭州  20   5284  青少年\n",
       "5   女   南京  34   9828   壮年\n",
       "6   男   成都  33   9366   壮年\n",
       "7   男   重庆  47  22820   中年\n",
       "8   男   武汉  36  16927   壮年\n",
       "9   女   西安  42  11591   中年\n",
       "10  男   南昌  42  11316   中年\n",
       "11  男   合肥  27   7426   青年\n",
       "12  男   长沙  23   4705   青年\n",
       "13  男   福州  41  24117   中年\n",
       "14  女   厦门  50  69153   中年\n",
       "15  女   济南  32   5559   壮年\n",
       "16  男   郑州  56   6391  中老年\n",
       "17  男   长春  60  11020  中老年\n",
       "18  男  哈尔滨  59  68189  中老年\n",
       "19  女   南宁  32  15661   壮年"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 定义分组标签列表：['儿童', '青少年', '青年', '壮年', '中年', '中老年', '老年']\n",
    "# 2. 根据前一个单元格里定义的分组对df1的年龄列进行分箱，并使用以上分组标签\n",
    "# 3. 最后为df1新建“年龄组”列，值为以上分组标签\n",
    "age_labels = ['儿童', '青少年', '青年', '壮年', '中年', '中老年', '老年']\n",
    "df1[\"年龄组\"] = pd.cut(df1[\"年龄\"], age_bins, labels=age_labels)\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cdc06edb-70f3-49ce-9d23-351ae6385528",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "年龄组\n",
       "儿童              NaN\n",
       "青少年     5284.000000\n",
       "青年      6169.000000\n",
       "壮年     12305.714286\n",
       "中年     24729.833333\n",
       "中老年    28533.333333\n",
       "老年              NaN\n",
       "Name: 工资, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对df1根据年龄组进行分组，计算各个年龄组的平均工资\n",
    "df1.groupby(\"年龄组\")[\"工资\"].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f36f4687-d395-4ab0-8410-01611ab502a0",
   "metadata": {},
   "source": [
    "## 重置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a9cb2485-de65-4b9d-aa04-86a59c3c1148",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1500</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2000</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3000</td>\n",
       "      <td>171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2500</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1800</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2200</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3200</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2700</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  分店编号     时间段  商品类别   销售额  销售数量\n",
       "0  001  2022Q1  生鲜食品  1500   105\n",
       "1  002  2022Q1  生鲜食品  2000    84\n",
       "2  001  2022Q1  休闲食品  3000   171\n",
       "3  002  2022Q1  休闲食品  2500   162\n",
       "4  001  2022Q2  生鲜食品  1800    67\n",
       "5  002  2022Q2  生鲜食品  2200   150\n",
       "6  001  2022Q2  休闲食品  3200    99\n",
       "7  002  2022Q2  休闲食品  2700    57"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({\n",
    "    '分店编号': ['001', '002', '001', '002', '001', '002', '001', '002'],\n",
    "    '时间段': ['2022Q1', '2022Q1', '2022Q1', '2022Q1', '2022Q2', '2022Q2', '2022Q2', '2022Q2'],\n",
    "    '商品类别': ['生鲜食品', '生鲜食品', '休闲食品', '休闲食品', '生鲜食品', '生鲜食品', '休闲食品', '休闲食品'],\n",
    "    '销售额': [1500, 2000, 3000, 2500, 1800, 2200, 3200, 2700],\n",
    "    '销售数量': [105,  84, 171, 162,  67, 150,  99,  57]\n",
    "})\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e5a742ec-b675-40f4-9f2e-5658226c3e4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">001</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2250.0</td>\n",
       "      <td>138.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2500.0</td>\n",
       "      <td>83.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">002</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2250.0</td>\n",
       "      <td>123.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2450.0</td>\n",
       "      <td>103.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                销售额   销售数量\n",
       "分店编号 时间段                  \n",
       "001  2022Q1  2250.0  138.0\n",
       "     2022Q2  2500.0   83.0\n",
       "002  2022Q1  2250.0  123.0\n",
       "     2022Q2  2450.0  103.5"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df2 = df2.groupby(['分店编号', '时间段'])[['销售额', '销售数量']].mean()\n",
    "grouped_df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3928f0ee-87ea-4154-a5d0-d33f44c22525",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2250.0</td>\n",
       "      <td>138.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2500.0</td>\n",
       "      <td>83.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           销售额   销售数量\n",
       "时间段                  \n",
       "2022Q1  2250.0  138.0\n",
       "2022Q2  2500.0   83.0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取grouped_df2索引为001的行\n",
    "grouped_df2.loc[\"001\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "30e040a7-9540-47e4-9e89-f24011dd1f6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "销售额     2250.0\n",
       "销售数量     138.0\n",
       "Name: 2022Q1, dtype: float64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取grouped_df2外层索引为001、内层索引为2022Q1的行\n",
    "grouped_df2.loc[\"001\"].loc[\"2022Q1\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "761b5c86-b41a-4117-82e6-1189502cc4b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>2250.0</td>\n",
       "      <td>138.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>2500.0</td>\n",
       "      <td>83.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>2250.0</td>\n",
       "      <td>123.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>2450.0</td>\n",
       "      <td>103.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  分店编号     时间段     销售额   销售数量\n",
       "0  001  2022Q1  2250.0  138.0\n",
       "1  001  2022Q2  2500.0   83.0\n",
       "2  002  2022Q1  2250.0  123.0\n",
       "3  002  2022Q2  2450.0  103.5"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置grouped_df2的索引\n",
    "grouped_df2.reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41315a9c-a830-4054-b30f-6f8957ccef64",
   "metadata": {},
   "source": [
    "## 根据条件筛选数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1ca017bc-6e30-405d-87dc-d9db0acdf600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>居住地</th>\n",
       "      <th>年龄</th>\n",
       "      <th>工资</th>\n",
       "      <th>年龄组</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>男</td>\n",
       "      <td>北京</td>\n",
       "      <td>38</td>\n",
       "      <td>18053</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>上海</td>\n",
       "      <td>42</td>\n",
       "      <td>9382</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>男</td>\n",
       "      <td>广州</td>\n",
       "      <td>23</td>\n",
       "      <td>6376</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>女</td>\n",
       "      <td>深圳</td>\n",
       "      <td>36</td>\n",
       "      <td>10746</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>杭州</td>\n",
       "      <td>20</td>\n",
       "      <td>5284</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>女</td>\n",
       "      <td>南京</td>\n",
       "      <td>34</td>\n",
       "      <td>9828</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>成都</td>\n",
       "      <td>33</td>\n",
       "      <td>9366</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>男</td>\n",
       "      <td>重庆</td>\n",
       "      <td>47</td>\n",
       "      <td>22820</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>男</td>\n",
       "      <td>武汉</td>\n",
       "      <td>36</td>\n",
       "      <td>16927</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>女</td>\n",
       "      <td>西安</td>\n",
       "      <td>42</td>\n",
       "      <td>11591</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>男</td>\n",
       "      <td>南昌</td>\n",
       "      <td>42</td>\n",
       "      <td>11316</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>男</td>\n",
       "      <td>合肥</td>\n",
       "      <td>27</td>\n",
       "      <td>7426</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>男</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23</td>\n",
       "      <td>4705</td>\n",
       "      <td>青年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>男</td>\n",
       "      <td>福州</td>\n",
       "      <td>41</td>\n",
       "      <td>24117</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>女</td>\n",
       "      <td>厦门</td>\n",
       "      <td>50</td>\n",
       "      <td>69153</td>\n",
       "      <td>中年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>女</td>\n",
       "      <td>济南</td>\n",
       "      <td>32</td>\n",
       "      <td>5559</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>男</td>\n",
       "      <td>郑州</td>\n",
       "      <td>56</td>\n",
       "      <td>6391</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>男</td>\n",
       "      <td>长春</td>\n",
       "      <td>60</td>\n",
       "      <td>11020</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>男</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>59</td>\n",
       "      <td>68189</td>\n",
       "      <td>中老年</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>女</td>\n",
       "      <td>南宁</td>\n",
       "      <td>32</td>\n",
       "      <td>15661</td>\n",
       "      <td>壮年</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  居住地  年龄     工资  年龄组\n",
       "0   男   北京  38  18053   壮年\n",
       "1   女   上海  42   9382   中年\n",
       "2   男   广州  23   6376   青年\n",
       "3   女   深圳  36  10746   壮年\n",
       "4   男   杭州  20   5284  青少年\n",
       "5   女   南京  34   9828   壮年\n",
       "6   男   成都  33   9366   壮年\n",
       "7   男   重庆  47  22820   中年\n",
       "8   男   武汉  36  16927   壮年\n",
       "9   女   西安  42  11591   中年\n",
       "10  男   南昌  42  11316   中年\n",
       "11  男   合肥  27   7426   青年\n",
       "12  男   长沙  23   4705   青年\n",
       "13  男   福州  41  24117   中年\n",
       "14  女   厦门  50  69153   中年\n",
       "15  女   济南  32   5559   壮年\n",
       "16  男   郑州  56   6391  中老年\n",
       "17  男   长春  60  11020  中老年\n",
       "18  男  哈尔滨  59  68189  中老年\n",
       "19  女   南宁  32  15661   壮年"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "01a0e200-3dc2-4937-9bd1-33b88f967794",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>居住地</th>\n",
       "      <th>年龄</th>\n",
       "      <th>工资</th>\n",
       "      <th>年龄组</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>杭州</td>\n",
       "      <td>20</td>\n",
       "      <td>5284</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  性别 居住地  年龄    工资  年龄组\n",
       "4  男  杭州  20  5284  青少年"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1[(df1[\"性别\"] == \"男\") & (df1[\"年龄\"] <= 20)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "82e6edbe-022d-486d-86e9-207d48d07d39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>居住地</th>\n",
       "      <th>年龄</th>\n",
       "      <th>工资</th>\n",
       "      <th>年龄组</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>杭州</td>\n",
       "      <td>20</td>\n",
       "      <td>5284</td>\n",
       "      <td>青少年</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  性别 居住地  年龄    工资  年龄组\n",
       "4  男  杭州  20  5284  青少年"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用query方法筛选出性别为男且年龄小于或等于20岁的观察值\n",
    "df1.query('(性别 == \"男\") & (年龄 <=20)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ded1977e-4759-40b4-9230-aa5881b81b54",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
